﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Controller;
using Entity;
namespace Web
{
    public partial class EditProduct : System.Web.UI.Page
    {
        private int productId;
        Helper helper = new Helper();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["id"] != null)
                {
                    productId = Convert.ToInt32(Request.QueryString["id"]);
                    ProductController productController = new ProductController();
                    ProductEntity productEntity = productController.FindProductById(productId);
                    ProductNameTextBox.Text = productEntity.Name;
                    ProductNoTextBox.Text = productEntity.No;
                    DescTextBox.Text = productEntity.Description;
                    RemarkTextBox.Text = productEntity.Remark;
                    RevTextBox.Text = productEntity.Revision;
                    VisibilityCheckBox.Checked = productEntity.Visibility;
                }
                else
                {
                    Response.Redirect("ProductList.aspx");
                }
            }
            this.PreRender += new EventHandler(UpdateFlashMessage);
        }

        private void UpdateFlashMessage(object sender, EventArgs e)
        {
            helper.ShowFlashMessage(FlashMessageLabel);
        }
        

        protected void UpdateButton_Click(object sender, EventArgs e)
        {
            try
            {
                int productId = Convert.ToInt32(Request.QueryString["id"]);
                ProductController productController = new ProductController();
                ProductEntity productEntity = new ProductEntity();
                productEntity.Id = Convert.ToInt32(Request.QueryString["id"]);
                productEntity.Name = ProductNameTextBox.Text;
                productEntity.No = ProductNoTextBox.Text;
                productEntity.Description = DescTextBox.Text;
                productEntity.Remark = RemarkTextBox.Text;
                productEntity.Revision = RevTextBox.Text;
                productEntity.Visibility = VisibilityCheckBox.Checked;
                productController.updateProduct(productEntity);
                //UpdateSuccessfuLabel.Text = "Update Product Successful!";
                Session["FlashMessage"] = "Success: Update Product Successful!";
                Session["FlashMessageType"] = "success";
                //Response.Redirect("ProductDetail.aspx?id=" + productId);
            }
            catch(Exception ex)
            {
                Session["FlashMessage"] = "Error: "+ex.Message;
                Session["FlashMessageType"] = "error";
            }
        }

        protected void CancelButton_Click(object sender, EventArgs e)
        {
            int productId = Convert.ToInt32(Request.QueryString["id"]);

            Response.Redirect("ProductDetail.aspx?id="+productId);
        }
    }
}